<template>
  <div class="app-container">
    <el-tag type="success">班级总数 ：{{ total }}</el-tag>
    <!--<el-tag type="danger" style="margin-left: 10px">未结课 ：{{ undone }}</el-tag>-->

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 900px;margin-top: 10px">
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="260px" label="班级名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" width="80px" label="班级类型">
        <template slot-scope="scope">
          <el-tag :type="scope.row.type | typeFilter">
            {{ scope.row.type }}
          </el-tag>
        </template>
      </el-table-column>

<!--      <el-table-column class-name="status-col" label="课程状态" width="110">-->
<!--        <template slot-scope="{row}">-->
<!--          <el-tag :type="row.status | statusFilter">-->
<!--            {{ row.status | nameFilter }}-->
<!--          </el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column width="120px" align="center" label="负责老师">
        <template slot-scope="scope">
          <span>{{ scope.row.teacher }}</span>
        </template>
      </el-table-column>

      <el-table-column width="160px" align="center" label="开班时间">
        <template slot-scope="scope">
          <span>{{ scope.row.startAt | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="编辑" width="auto">
        <template slot-scope="scope">
          <!--          <router-link :to="'/example/edit/'+scope.row.id">-->
          <el-button type="primary" size="small" @click="dialogTable(scope.row)">查看</el-button>
          <el-button type="danger" size="small" @click="deleteClass(scope.row.id)">删除</el-button>
          <!--          </router-link>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog :visible.sync="dialogVisible" :title="className" @dragDialog="handleDrag">
      <!--          <el-select ref="select" v-model="value" placeholder="请选择">-->
      <!--              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />-->
      <!--          </el-select>-->
      <el-table v-loading="stuListLoading" :data="stulist" border fit highlight-current-row style="width: 521px;margin-top: 10px">
        <el-table-column align="center" width="120px" label="名称">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="140px" label="学号">
          <template slot-scope="scope">
            <span>{{ scope.row.learn_number }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120px" label="年级">
          <template slot-scope="scope">
            <span>{{ scope.row.grade }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="140px" label="加入时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdAt | parseTime('{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!--            <el-table-column property="" align="center" width="120px" label="年级" />-->
        <!--            <el-table-column property="" align="center" width="149px" label="操作" />-->
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { fetchClassList, deleteClass } from '../../api/class'
import { fetchStudentList } from '../../api/student'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'ClassManager',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        'done': 'success',
        'undone': 'danger'
      }
      return statusMap[status]
    },
    // nameFilter(status) {
    //   const statusMap = {
    //     'done': '已结课',
    //     'undone': '未结课'
    //   }
    //   return statusMap[status]
    // },
    typeFilter(status) {
      return 'success'
    }
  },
  data() {
    return {
      stulist: null,
      className: '',
      dialogVisible: false,
      undone: 0,
      list: null,
      total: 0,
      listLoading: true,
      stuListLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      stuListQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        classID: -1,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchClassList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.undone = 0
        this.list.forEach((item) => {
          if (item.status === 'undone') this.undone += 1
        })
        this.listLoading = false
      })
    },
    deleteClass(classID) {
      this.$confirm('此操作将删除该班级记录, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteClass(classID).then(() => {
          this.$message({
            type: 'success',
            message: '删除班级成功!'
          })
          // 重新获取班级列表
          this.getList()
        }).catch(() => {
          this.$message({
            type: 'danger',
            message: '删除班级失败！'
          })
        })
      }).catch(() => {

      })
    },
    handleDrag() {
      this.$refs.select.blur()
    },
    dialogTable(row) {
      this.className = row.name
      this.stuListQuery.classID = row.id
      this.stuListLoading = true
      fetchStudentList(this.stuListQuery).then(response => {
        this.stulist = response.data.items
        // this.total = response.data.total
        this.stuListLoading = false
      })
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>

</style>
